package com.bocloud.cmp.driver.convertor.os;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import org.openstack4j.model.image.Image;

import com.bocloud.cmp.driver.convertor.BeanConvert;
import com.bocloud.cmp.model.ImageModel;
import com.google.common.collect.Lists;

public class ConvertImage implements BeanConvert<ImageModel, Image> {

	@Override
	public List<ImageModel> convertList(List<? extends Image> images) {
		List<ImageModel> imageModels = Lists.newArrayList();
		for (Image image : images) {
			imageModels.add(convertModel(image));
		}
		return imageModels;
	}

	@Override
	public ImageModel convertModel(Image image) {
		if (null == image) {
			return null;
		}
		BigDecimal size = new BigDecimal(0);// edit by ouyangwulin
		ImageModel model = new ImageModel();
		model.setGmtCreate(image.getCreatedAt());
		model.setImageId(image.getId());
		model.setMinDisk(image.getMinDisk());
		model.setName(image.getName());
		if (null != image.getSize()) {
			size = new BigDecimal(image.getSize()).divide(new BigDecimal(1024 * 1024 * 1024), 2, RoundingMode.HALF_UP);
		}
		model.setSize(size.doubleValue());
		model.setStatus(image.getStatus().name());
		return model;
	}

}
